package com.zhuannimei.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.umeng.analytics.MobclickAgent;
import com.zhuannimei.R;
import com.zhuannimei.utility.MessageHandlerList;
import com.zhuannimei.utility.UserData;
import com.zhuannimei.view.HandyTextView;
import com.zhuannimei.view.LoadingDialog;
import com.zhuannimei.view.TitlePane;
import com.zhuannimei.view.ZnmLoadingDialog;

public class BaseActivity extends FragmentActivity {
	public static final int UI_FINISH = 11000;
	public Handler mHandler;

	protected ZnmLoadingDialog mLoadingDialog;

	FragmentManager suManager;
	public TitlePane mTitlePane;
	public String userId = UserData.getInstance().getUserUserId().trim();
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	public LoadingDialog pd;
	DisplayImageOptions options = new DisplayImageOptions.Builder()
			.showImageForEmptyUri(R.drawable.ic_launcher)
			.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
			.cacheOnDisc(true).displayer(new RoundedBitmapDisplayer(1)).build();

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		suManager = getSupportFragmentManager();
		pd = new LoadingDialog(this);
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				handleMsg(msg);
			}
		};
		String className = this.getClass().getName();
		MessageHandlerList.addHandler(className, mHandler);
		mLoadingDialog = new ZnmLoadingDialog(this, "请求提交中");
	}

	protected void showLoadingDialog(String text) {
		if (text != null) {
			mLoadingDialog.setText(text);
		}
		mLoadingDialog.show();
	}

	protected void dismissLoadingDialog() {
		if (mLoadingDialog.isShowing()) {
			mLoadingDialog.dismiss();
		}
	}

	protected void handleMsg(Message msg) {
		switch (msg.what) {
		case UI_FINISH:
			finish();
			break;
		default:
			break;
		}

	}

	@Override
	protected void onStart() {
		super.onStart();
		if (mTitlePane == null) {
			mTitlePane = new TitlePane(BaseActivity.this);
		}
	}

	@Override
	protected void onDestroy() {
		try {
			super.onDestroy();
			// 移除消息池中对应的handler
			String className = this.getClass().getName();
			MessageHandlerList.removeHandler(className);
			// 要取消广播
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** 显示自定义Toast提示(来自String) **/
	protected void showCustomToast(String text) {
		View toastRoot = LayoutInflater.from(BaseActivity.this).inflate(
				R.layout.common_toast, null);
		((HandyTextView) toastRoot.findViewById(R.id.toast_text)).setText(text);
		Toast toast = new Toast(BaseActivity.this);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.setView(toastRoot);
		toast.show();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}
	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
